package com.huashi.dealer.commonEntity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.huashi.dealer.core.handler.HtmlContentTypeHandler;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 文章记录表
 * 对应数据库表：yoshop_article
 * 
 * @author 系统生成
 * @since 2025-01-27
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Schema(description = "文章记录表")
@TableName("yoshop_article")
public class Article extends BaseModel implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 文章id
     */
    @Schema(description = "文章id")
    @TableId(value = "article_id", type = IdType.AUTO)
    private Integer articleId;

    /**
     * 文章标题
     */
    @Schema(description = "文章标题")
    @TableField("article_title")
    private String articleTitle;

    /**
     * 列表显示方式(10小图展示 20大图展示)
     */
    @Schema(description = "列表显示方式(10小图展示 20大图展示)")
    @TableField("show_type")
    private Integer showType;

    /**
     * 文章分类id
     */
    @Schema(description = "文章分类id")
    @TableField("category_id")
    private Integer categoryId;

    /**
     * 封面图id
     */
    @Schema(description = "封面图id")
    @TableField("image_id")
    private Integer imageId;

    /**
     * 文章内容
     */
    @Schema(description = "文章内容")
    @TableField(value = "article_content",typeHandler = HtmlContentTypeHandler .class)
    private String articleContent;

    /**
     * 文章排序(数字越小越靠前)
     */
    @Schema(description = "文章排序(数字越小越靠前)")
    @TableField("article_sort")
    private Integer articleSort;

    /**
     * 文章状态(0隐藏 1显示)
     */
    @Schema(description = "文章状态(0隐藏 1显示)")
    @TableField("article_status")
    private Integer articleStatus;

    /**
     * 虚拟阅读量(仅用作展示)
     */
    @Schema(description = "虚拟阅读量(仅用作展示)")
    @TableField("virtual_views")
    private Integer virtualViews;

    /**
     * 实际阅读量
     */
    @Schema(description = "实际阅读量")
    @TableField("actual_views")
    private Integer actualViews;

    /**
     * 是否删除
     */
    @Schema(description = "是否删除")
    @TableField("is_delete")
    @TableLogic
    private Integer isDelete;

    /**
     * 创建时间
     */
    @Schema(description = "创建时间")
    @TableField("create_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Integer createTime;

    /**
     * 更新时间
     */
    @Schema(description = "更新时间")
    @TableField("update_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Integer updateTime;

    // ==================== 业务方法 ====================

    /**
     * 获取总浏览量（虚拟+实际）
     */
    @Schema(description = "总浏览量")
    public Integer getShowViews() {
        return (virtualViews != null ? virtualViews : 0) + (actualViews != null ? actualViews : 0);
    }

    /**
     * 判断是否显示
     */
    @Schema(description = "是否显示")
    public Boolean isVisible() {
        return articleStatus != null && articleStatus == 1;
    }

    /**
     * 判断是否隐藏
     */
    @Schema(description = "是否隐藏")
    public Boolean isHidden() {
        return articleStatus != null && articleStatus == 0;
    }

    /**
     * 判断是否小图展示
     */
    @Schema(description = "是否小图展示")
    public Boolean isSmallImage() {
        return showType != null && showType == 10;
    }

    /**
     * 判断是否大图展示
     */
    @Schema(description = "是否大图展示")
    public Boolean isLargeImage() {
        return showType != null && showType == 20;
    }

    /**
     * 获取显示类型描述
     */
    @Schema(description = "显示类型描述")
    public String getShowTypeDesc() {
        if (showType == null) return "未知";
        switch (showType) {
            case 10: return "小图展示";
            case 20: return "大图展示";
            default: return "未知";
        }
    }

    /**
     * 获取状态描述
     */
    @Schema(description = "状态描述")
    public String getStatusDesc() {
        if (articleStatus == null) return "未知";
        switch (articleStatus) {
            case 0: return "隐藏";
            case 1: return "显示";
            default: return "未知";
        }
    }

    /**
     * 格式化创建时间
     */
    @Schema(description = "格式化创建时间")
    public String getCreateTimeStr() {
        if (createTime == null) return "";
        return new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                .format(new java.util.Date(createTime * 1000L));
    }

    /**
     * 格式化更新时间
     */
    @Schema(description = "格式化更新时间")
    public String getUpdateTimeStr() {
        if (updateTime == null) return "";
        return new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                .format(new java.util.Date(updateTime * 1000L));
    }

    /**
     * 格式化创建日期（仅日期）
     */
    @Schema(description = "格式化创建日期")
    public String getCreateDateStr() {
        if (createTime == null) return "";
        return new java.text.SimpleDateFormat("yyyy-MM-dd")
                .format(new java.util.Date(createTime * 1000L));
    }

    // ==================== 枚举值 ====================

    /**
     * 显示方式枚举
     */
    public static class ShowType {
        public static final Integer SMALL_IMAGE = 10;  // 小图展示
        public static final Integer LARGE_IMAGE = 20;  // 大图展示
    }

    /**
     * 文章状态枚举
     */
    public static class Status {
        public static final Integer HIDDEN = 0;  // 隐藏
        public static final Integer VISIBLE = 1;  // 显示
    }
} 